package com.zkingsoft.web.tag;

import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.StringUtils;
import com.zkingsoft.web.bean.Product;
import com.zkingsoft.web.bean.ProductAttrDef;
import com.zkingsoft.web.bean.ProductAttrRef;
import com.zkingsoft.web.dao.ProductAttrDefDao;
import com.zkingsoft.web.dao.ProductAttrRefDao;
import com.zkingsoft.web.dao.ProductDao;
import com.zkingsoft.web.tool.SystemConstance;
import com.zkingsoft.web.tool.Tools;
import com.zkingsoft.web.tool.WebUtil;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class ProductsTag extends SimpleTagSupport {

	/**
	 * 属性编码
	 */

	private String code;

	private String code2;



	/**
	 * 文字搜索
	 */
	private String name;
	private String var;

	// 分页大小
	private Integer pageSize=10;
	// 当前分页
	private Integer pageNow=0;

	/**
	 * 获取产品
	 * 
	 * @author jiangyouyao
	 */
	public void doTag() throws JspException, IOException {
		ProductDao productDao = WebUtil.getService(this.getJspContext(), SystemConstance.PRODUCTDAO);
		ProductAttrRefDao productAttrRefDao = WebUtil.getService(this.getJspContext(), SystemConstance.PRODUCT_ATTRREFDAO);
		ProductAttrDefDao productAttrDefDao = WebUtil.getService(this.getJspContext(), SystemConstance.PRODUCT_ATTRDEFDAO);

		String companyId=Tools.getCompanyId();
		PaginationVO pageVo = new PaginationVO();
		if (pageSize != null) {
			pageVo.setLimit(pageSize);
		}
		if (pageNow != null) {
			pageVo.setOffset(pageSize * pageNow);
			if(StringUtils.isBlank(code)){
				code=null;
			}



			int recodeCount = productDao.selectCountBYCodeOrName(companyId,code,name);
			int pageCount = 0;
			if (recodeCount % pageSize == 0) {
				pageCount = recodeCount / pageSize;
			} else {
				pageCount = recodeCount / pageSize + 1;
			}
			this.getJspContext().setAttribute("recodeCount", pageCount);
		}
		List<Product> productList= productDao.selectBYCodeOrName(companyId,code,name,pageSize,pageVo.getOffset());
		List<Product> productList2=new ArrayList<>();
		//如果存在code2则在程序中进行赛选
		if(StringUtils.isNotBlank(code2)){
			ProductAttrRef productAttrRef = new ProductAttrRef();
			productAttrRef.setIsSku(ProductAttrRef.NO_SKU);

			for(Product p:productList){
				//查询产品所有的属性
				productAttrRef.setProductId(p.getId());
				List<ProductAttrRef> attrs = productAttrRefDao.selectByModel(productAttrRef);
				for (ProductAttrRef ref  :attrs) {
					ProductAttrDef def= productAttrDefDao.selectById(ref.getAttrId());
					if(code2.equals(def.getAttrCode())){
						productList2.add(p);
					}

				}
			}
			this.getJspContext().setAttribute(var, productList2);
		}else{
			this.getJspContext().setAttribute(var, productList);
		}

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	
	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getPageNow() {
		return pageNow;
	}

	public void setPageNow(Integer pageNow) {
		this.pageNow = pageNow;
	}


	public String getVar() {
		return var;
	}

	
	public void setVar(String var) {
		this.var = var;
	}

	public String getCode2() {
		return code2;
	}

	public void setCode2(String code2) {
		this.code2 = code2;
	}

	StringWriter sw = new StringWriter();

}